<?php
	/**
	* Classe Model do topico
	* @author Emeson Santana
	*/
	
	class Topic extends AppModel {
		public $name = "Topic";
		public $useTable = "topics";
		public $belongsTo = array("Community", "User");
		public $hasMany = array(
		        'Post' => array(
		            'className' => 'Post',
		            'order' => 'Post.creation_date DESC'
		        )
		    );
		#public function delete($topic_id){
			# code...
		#}
		#
		public function getIdByUrl($url){
			$result = $this->query('SELECT id FROM topics AS Topic WHERE url = "'.$url.'"');
			if(empty($result)){
				return $result;
			}
			else {
				return $result[0]['Topic']['id'];
			}
		}

		public function getUrlById($id){
			$result = $this->query("SELECT Topic.url FROM topics AS Topic WHERE id = '".$id."'");
			return $result[0]['Topic']['url'];
		}

        /**
         * Buscar os quatro ultimos comentarios do topico
         * @param int $id Identificador do topico
         * @return type Array Posts de uma Topico determinado
         */
        public function getPostsFeed($id){
            $sql = "SELECT posts.id AS post_id, posts.description AS post_description, posts.user_id AS user_id, posts.creation_date AS creation_date, posts.topic_id FROM topics
                        INNER JOIN posts ON topics.id = posts.topic_id 
                        WHERE posts.topic_id = $id
                        LIMIT 4";
            $result = $this->query($sql);
            return $result;
        }
	}
?>